package tools.shapes;

import java.awt.Point;
import java.util.Vector;

import app.DrawingCanvas;
import java.awt.Color;

/**
 * This class contains the methods and parameters necessary for drawing and storing
 * freehand shapes.
 *
 */

public class FreehandShape extends Shape {

	Vector<Point> pointVector = new Vector<Point>();

	public void addPoint(Point p) {
		if (pointVector.isEmpty()) {
			this.xMin = p.x;
			this.xMax = p.x;
			this.yMin = p.y;
			this.yMax = p.y;
		}
		else {
			if (p.x < this.xMin) {
				this.xMin = p.x;
			}
			if (p.x > this.xMax) {
				this.xMax = p.x;
			}
			if (p.y < this.yMin) {
				this.yMin = p.y;
			}
			if (p.y > this.yMax) {
				this.yMax = p.y;
			}
		}
		
		pointVector.add(p);
	}
	
	public void drawSegment(DrawingCanvas c, Point start, Point end) {
		c.getimageBufferGraphics().drawLine(start.x, start.y, end.x, end.y);
	}
	
	public void draw(DrawingCanvas c) {
		Color savedColor = c.getpenColor();
		c.getimageBufferGraphics().setColor(this.color);
		for (int i = 0; i < pointVector.size() - 1; i++) {
			drawSegment(c, pointVector.get(i), pointVector.get(i+1));
		}
		c.getimageBufferGraphics().setColor(savedColor);
	}
	
	public void drawNext(DrawingCanvas c) {
		if (pointVector.size() >= 2) {
			drawSegment(c, pointVector.get(pointVector.size() - 1),
					       pointVector.get(pointVector.size() - 2));
		}
	}

	public void drawOutline(DrawingCanvas c) {
		draw(c);		
	}

	public void resize(Point startPos, Point newPos) {
		// This does nothing. We do not support resizing freehand shapes.
	}

	public void move(Point startPos, Point newPos) {
		int deltaX = newPos.x - startPos.x;
		int deltaY = newPos.y - startPos.y;

		xMin += deltaX;
		xMax += deltaX;
		yMin += deltaY;
		yMax += deltaY;
		
		for (Point p : pointVector) {
			p.setLocation(p.x + deltaX, p.y + deltaY);
		}
	}
	
}
